﻿using System.Web;
using System.Web.SessionState;

namespace Vidh.SimplyToDo.Controllers.Helpers
{
    public class SessionHelper : ISessionHelper
    {
        private HttpSessionState _sessionState;

        public SessionHelper()
        {
            _sessionState = HttpContext.Current.Session;
        }

        public SessionHelper(HttpSessionState sessionState)
        {
            _sessionState = sessionState;
        }

        public int CurrentUserID
        {
            get
            {
                int id = 0;
                object value = GetKeyValue("UserID");
                if (value != null)
                {
                    int.TryParse(value.ToString(), out id);
                }
                return id;
            }
        }

        public void SetKeyValue(string keyName, object value)
        {
            if (SessionExists)
            {
                _sessionState.Add(keyName, value);
            }
        }

        public void Remove(string keyName)
        {
            if (SessionExists)
            {
                _sessionState.Remove(keyName);
            }
        }

        public object GetKeyValue(string keyName)
        {
            if (SessionExists)
            {
                return _sessionState[keyName];
            }

            return null;
        }

        public bool SessionExists
        {
            get
            {
                return _sessionState != null;
            }
        }
    }
}